package com.petdog.module.report.service.impl;

import com.petdog.common.dto.PageResult;
import com.petdog.infrastructure.entity.Report;
import com.petdog.infrastructure.mapper.ReportMapper;
import com.petdog.module.report.dto.ReportProcessRequest;
import com.petdog.module.report.dto.ReportRequest;
import com.petdog.module.report.dto.ReportDto;
import com.petdog.module.report.service.ReportService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 举报服务实现类
 */
@Service
public class ReportServiceImpl implements ReportService {

    @Resource
    private ReportMapper reportMapper;

    @Transactional
    @Override
    public Long submitReport(ReportRequest request, Long reporterId) {
        // 检查是否已经举报过相同内容
        long existingCount = reportMapper.countList(
                request.getContentType() != null ? request.getContentType().toString() : null, // 转换为String类型
                request.getContentId(), 
                null, // 不限制状态
                null, 
                null
        );
        
        if (existingCount > 0) {
            throw new IllegalArgumentException("您已举报过该内容");
        }
        
        Report report = new Report();
        BeanUtils.copyProperties(request, report);
        report.setReporterId(reporterId);
        report.setStatus(1); // 待处理状态
        report.setCreateTime(LocalDateTime.now());
        
        reportMapper.insert(report);
        return report.getReportId();
    }

    @Override
    public ReportDto getReportById(Long reportId) {
        Report report = reportMapper.selectById(reportId);
        if (report == null) {
            throw new IllegalArgumentException("举报记录不存在");
        }
        
        return convertToDto(report);
    }

    @Override
    public PageResult<ReportDto> getReportList(Integer pageNum, Integer pageSize, Long reporterId, Integer contentType, Long contentId, Integer reportType, Integer status) {
        int offset = (pageNum - 1) * pageSize;
        List<Report> reports = reportMapper.selectList(
                offset, 
                pageSize, 
                contentType != null ? contentType.toString() : null, // 转换为String类型
                contentId, 
                status, 
                null, 
                null
        );
        long total = reportMapper.countList(
                contentType != null ? contentType.toString() : null, // 转换为String类型
                contentId, 
                status, 
                null, 
                null
        );
        
        List<ReportDto> dtoList = reports.stream()
                .map(this::convertToDto)
                .collect(Collectors.toList());
        
        return new PageResult<>(dtoList, (int)total, pageNum, pageSize);
    }

    @Transactional
    @Override
    public boolean processReport(ReportProcessRequest request, Long processorId) {
        Report report = reportMapper.selectById(request.getReportId());
        if (report == null) {
            throw new IllegalArgumentException("举报记录不存在");
        }
        
        if (report.getStatus() != 1) { // 不是待处理状态
            throw new IllegalArgumentException("该举报已处理");
        }
        
        report.setStatus(request.getStatus());
        report.setHandlerId(processorId);
        report.setReviewNote(request.getRemark());
        report.setHandleTime(LocalDateTime.now());
        
        // 修复updateStatus方法调用，添加缺失的handleTime参数
        return reportMapper.updateStatus(
                report.getReportId(), 
                request.getStatus(), 
                processorId, 
                request.getRemark(),
                LocalDateTime.now()
        ) > 0;
    }

    @Override
    public long countReports(Integer status) {
        return reportMapper.countList(null, null, status, null, null);
    }

    @Override
    public int getReportCountByContent(Integer contentType, Long contentId, Integer status) {
        // 先检查状态是否为null，不为null时使用countList
        if (status != null) {
            return (int) reportMapper.countList(contentType.toString(), contentId, status, null, null);
        }
        // 状态为null时使用countByContent
        return reportMapper.countByContent(contentType.toString(), contentId);
    }
    
    /**
     * 将Report实体转换为ReportDto
     */
    private ReportDto convertToDto(Report report) {
        ReportDto dto = new ReportDto();
        BeanUtils.copyProperties(report, dto);
        return dto;
    }
}